#  This file is part of OpenLightingDesigner.
# 
#  OpenLightingDesigner is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  OpenLightingDesigner is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenLightingDesigner.  If not, see <http://www.gnu.org/licenses/>.
#
# playback.py
# Copyright (c) 2010 - 2011 Matthew Reid

from Bases import setID
from Bases import OSCBaseObject
from DeviceSystem.Groups.DeviceGroup import DeviceGroup

class Playback(OSCBaseObject):
    _Properties = {'name':dict(type=str), 'index':dict(type=int), 'value':dict(default=100, fvalidate='_validate_value')}
    def __init__(self, **kwargs):
        self.MainController = kwargs.get('MainController')
        self.DeviceSystem = self.MainController.DeviceSystem
        super(Playback, self).__init__(**kwargs)
        self.register_signal('value_changed')
        if 'deserialize' not in kwargs:
            self.id = setID(kwargs.get('id'))
            self.name = kwargs.get('name')
            self.index = kwargs.get('index')
        self.value_min = 0
        self.value_max = 100
        self.stack = kwargs.get('stack')
        self.group = DeviceGroup(MainController=self.MainController, 
                                 submaster=True, 
                                 osc_address='DeviceGroup', 
                                 name='_'.join([self.name, 'DeviceGroup']), 
                                 osc_parent_node=self.osc_node, 
                                 root_category=self.DeviceSystem.root_category.subcategories['device'])
        self.update_snapshots()
        self.stack.set_playback(self)
        self.stack.connect('new_snapshot', self.on_new_snapshot)
        self.stack.connect('sequencer_state', self.on_stack_sequencer_state)
        self.bind(value=self.on_value_set)
        
    def on_value_set(self, **kwargs):
        value = kwargs.get('value')
        self.group.AttributeGroups['Dimmer'].value = value
        self.emit('value_changed', playback=self, value=value)
        
    def _validate_value(self, value):
        return value >= 0 and value <= 100
        
    def update_snapshots(self, key=None):
        if key:
            if key not in self.stack.Snapshots:
                self.group.del_member[key]
                return
            snapshots = {key:self.stack.Snapshots[key]}
        else:
            snapshots = self.stack.Snapshots
        for key, val in snapshots.iteritems():
            for mkey, mval in val.members.iteritems():
                group = getattr(mval, 'parent_group', None)
                if group and group.id not in self.group.members:
                    self.group.add_member(**{group.id:group})
        print 'playback members:', self.group.members
        
    def on_new_snapshot(self, **kwargs):
        key = kwargs.get('id')
        self.update_snapshots(key)
        
    def on_stack_sequencer_state(self, **kwargs):
        state = kwargs.get('state')
        sn = kwargs.get('snapshot')
        if state == False:
            members = {}
            for m in sn.members.itervalues():
                group = getattr(m, 'parent_group', None)
                if group and group.id not in members:
                    members[group.id] = group
            self.group.AttributeGroups['Dimmer'].reset_member_scales(members=members)
